var util = require('../../../utils/util.js');
var ajax = require('../../../utils/ajax.js');
var common = require('../../../utils/common.js');
var PageBase = require('../../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    shopid:null,
    shopname: null,
    dataLoaded: false,
    userInfo: {},
    shopInfoData:{},
    //banner
    banner: [],
    notice: [],
    ad: {},
    //拼团商品
    ptgoods: [],
    msgoods: [],
    //热销商品
    pbgoods: []
  },

  onShareAppMessage: function (res) {
    return {
      title:  '欢迎来到' + this.data.shopname,
      path: '/pages/shop/home/index?id=' + this.data.shopid,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  onLoad(options) {
    //debugger;
    var shopid = "";
    var recuid = "";
    if (typeof(options.scene) != "undefined" && options.scene != ''){
      var scene = decodeURIComponent(options.scene);
      var m = scene.split(",");
      shopid = m[0];
      recuid = m[1];
      app.setRecuid(recuid);
      //wx.setStorageSync("recuid", recuid);
    }
    else{
      shopid = options.id;
    }
    common.initApp({
      loginSuc: () => {
        this.setData({
          shopid
        })
        this._fetch();
      }
    });

    //this._fetch();
  },

  onPullDownRefresh() {
    this._fetch(() => {
      wx.stopPullDownRefresh();
    });
  },

  onReachBottom() {
    //this._getActList();
  },

  /**
   * 拉取页面数据
   * @return {[type]} [description]
   */
  _fetch(cb) {
    ajax.G({
      url: 'm=shop&c=index&a=indexsetting',
      data: {
        id: this.data.shopid
      },
      success: (res) => {
        //debugger;
        var {
          banner,
          lowprice,
          salead,
          secondsale,
          todaysale,
          bannerids,
          activity,
          lowpriceids,
          secondsaleids,
          todaysaleids,
          infoall
        } = res.data;
        
        var shopname = infoall.name;
        var shopid = infoall.shopid;
        var newactivity = {};
        newactivity.data = activity;
        newactivity.shopname = shopname;
        newactivity.shopid = this.data.shopid;
        // activity.map((item) => {
        //   item.shopname = shopname;
        // });
        console.log(activity);
        this.setData({
          dataLoaded: true,
          banner,
          lowprice,
          salead,
          secondsale,
          todaysale,

          bannerids,
          activity: newactivity,
          lowpriceids,
          secondsaleids,
          todaysaleids,
          infoall,
          shopname
        });
        wx.setNavigationBarTitle({
          title: this.data.shopname
        });
        cb && cb();
      },
      fail: () => {
        cb && cb();
      }
    });
  },

}));